<?php
namespace app\index\controller;

use think\Controller;
use think\Session;

class Article extends Controller
{
    public function _initialize()
    {
        $id = session('user');

        if (!$id) {
            $this->redirect('login/login/index');
        }
    }
	
    public function index()
    {
        $code       =   input('code');
        $msg        =   input('msg');

            $list      =   db('article')->order('sort asc')->paginate(10);
        return view('index',[
            'msg'   =>  $msg,
            'list'  =>  $list,
            'code'  =>  $code
        ]);
    }

    public function add()
    {
        $code   =   input('msg'); 
        $name      =   input('name');
        $sort      =  input('sort');
        //var_dump($code);
        return view('add',
            [
                'code'  =>  $code,
                'name'  =>  $name,
                'sort'  =>  $sort
            ]);
    }

    public function update()
    {
        $code   =   input('msg');
        $data   =   db('article')->where('id',input('id'))->find();
        $name      =   input('name');
        $sort      =  input('sort');
        return view('update',
            [
                'data'  =>  $data,
                'code'  =>  $code,
                'name'  =>  $name,
                'sort'  =>  $sort
            ]);
    }

    public function del()
    {   
        $dt   =   db('article')->where('id',input('id'))->find();
        $data   =   db('article')->where('id',input('id'))->delete();
        return redirect('/index/article/index',['code'=>1,'msg'=>'删除成功']);
    }

    public function create()
    {
        $insert['content']  =   input('content');
        $insert['title']  =   input('title');
        $insert['sort']  =  input('sort');
        if(db('article')->insert($insert)!==false)
        {
            return redirect('article/index');
        }else{
            unlink($url);
            return redirect('article/add',['code'=>0,'msg'=>'添加失败']);
        }

    }

    public function edit()
    {
        $insert['content']  =   input('content');
        $insert['title']  =   input('title');
        $insert['sort']  =  input('sort');
        if(db('article')->where('id',input('id'))->update($insert)!==false)
        {
            return redirect('article/index',['code'=>1,'msg'=>'添加成功']);
        }else{
            unlink($url);
            return;
        }
    }

}